var pnum = "";   
var initData = function(num){
	    var param = {
			page_num:num,
			page_size:"10"
	    }
		$.ajax({
	        url: base + "/dataCenter/businessdata/findEquipmentType",
	        data: param,
	        success: function (data, textStatus, jqXHR) {
	        	var pageCount = data.pager.pageCount;
	        	var dataObj ={
	        			"list":data.list,
	         			"dataTool":{
	         				"title":{
	          			      	"equipment_type_code": "设备类型代码"
	          			      	,"equipment_type_name": "设备类型名称"
	          			      	,"create_time":"创建时间"
	          			      	,"update_time":"修改时间"
	          			      	,"operation":"操作"
	            			},
	            			"titleCheckbox":false,
							"btn":[{"event":"updateObj","name":"编辑"},
							       {"event":"delObj","name":"删除"}]
						}
		  		};
	        	showPageTable('view', dataObj,num,pageCount,"initData");
	        },
	        error: function () {
	        }
	    });
  }

   var searchObj = function (num) {
       if (num==undefined|| num==""||num.type=="click" ) {
           num=1;
       }
       var name=$("#searchName").val();
       var param = {
           page_num:num,
           page_size:"10",
           name:name
       }
       $.ajax({
           url: base + "/dataCenter/businessdata/findEquipmentType",
           data: param,
           success: function (data, textStatus, jqXHR) {
               var pageCount = data.pager.pageCount;
               var dataObj ={
                   "list":data.list,
                   "dataTool":{
                       "title":{
                           "equipment_type_code": "设备类型代码"
                           ,"equipment_type_name": "设备类型名称"
                           ,"create_time":"创建时间"
                           ,"update_time":"修改时间"
                           ,"operation":"操作"
                       },
                       "titleCheckbox":false,
                       "btn":[{"event":"updateObj","name":"编辑"},
                           {"event":"delObj","name":"删除"}]
                   }
               };
               showPageTable('view', dataObj,num,pageCount,"searchObj");
           },
           error: function () {
           }
       });
   }

   var delObj = function(obj){
	   layer.confirm('确认删除?',{btn : [ '确定', '取消' ]},function(index){
		   layer.close(index);
		   var param = {id:obj.id}
		   $.ajax({
			   url:base+"/dataCenter/businessdata/delEquipmentTypeById",
			   data:param,
			   success:function(data,status){
				    if(data.statusCode == 0){
						layer.msg(data.message, {
							icon : 1
						});
				    }else{
						layer.msg(data.message, {
							icon : 5
						});
				    }
				    initData(pnum);
			   }
		   });
	   });
   }
   
   var addObj = function(){
	   var contentDIV = function(){
		   var content;
		   var data = {
				   "title":{
					   "equipment_type_code":"设备类型代码",
					   "equipment_type_name":"设备类型名称",
				   },"text":{
					   "equipment_type_code":"",
					   "equipment_type_name":"",
				   },"input":{
					   "equipment_type_code":"text",
					   "equipment_type_name":"text",
				   },"input_init":{
				   }
		   };
		   var getDiv = dc_update.innerHTML;
		   laytpl(getDiv).render(data,function(html){
			   content = html;
		   });
		   return content;
	   }
	   layopen(contentDIV());
   }
   
   var updateObj = function(obj){
	   var contentDIV = function(){
		   var content;
		   var data = {
				   "title":{
					   "id":"唯一标识",
					   "equipment_type_code":"设备类型代码",
					   "equipment_type_name":"设备类型名称",
				   },"text":{
					   "id":obj.id,
					   "equipment_type_code":obj.equipment_type_code,
					   "equipment_type_name":obj.equipment_type_name,
				   },"input":{
					   "id":"hidden",
					   "equipment_type_code":"text",
					   "equipment_type_name":"text",
				   },"input_init":{
				   }
		   };
		   var getDiv = dc_update.innerHTML;
		   laytpl(getDiv).render(data,function(html){
			   content = html;
		   });
		   return content;
	   }
	   layopen(contentDIV());
   }
   
   
   var layopen = function(content){
		layer.open({
		  	title:'更新设备类型'
			,content: content
	    	,area: ['800px', '500px']
		    ,scrollbar: false
		  	,btn: ['确认', '取消']
			,yes: function(index,layero){ //确认
				var data = $(layero).find("form").serializeJSON();
				if(data.equipment_type_code == ""){
					var obj = $("input[name='equipment_type_code']");
					obj.focus();
					layer.tips('请填写此字段!', obj);
					return false;
				}
				if(data.equipment_type_name == ""){
					var obj = $("input[name='equipment_type_name']");
					obj.focus();
					layer.tips('请填写此字段!', obj);
					return false;
				}
				$.post(base+"/dataCenter/businessdata/updateEquipmentType",{
					id:data.id==null?"":data.id,
					equipment_type_code:$.trim(data.equipment_type_code),
					equipment_type_name:$.trim(data.equipment_type_name)
				},
		  		function (data,status){
					if(data!=""&&data!=null){
						if(data.statusCode == 0){
							layer.msg('更新成功', {icon: 1,time:2000});
						}else{
							layer.msg(data.message, {icon: 2,time:2000});
						}
					}else{
						layer.msg('更新失败!', {icon: 2,time:2000});
					}
					initData(pnum);
		  		 })
			}
	});
  }
   $(document).ready(function(){
	   initData(1);
	   $("#addData").click(addObj);
       $("#searchInfo").click(searchObj);
   });
   